package com.gorkr.dp;

import org.junit.jupiter.api.Test;

/**
 * @author gorkr
 * @date 2022/06/16 10:03
 **/
public class H72EditDistance {

    public int minDistance(String word1, String word2) {

        // 整体偏移一位？
        // dp[i][j] = word1[...i-1] 转换喂 word2[...j-1] 所需的最小操作数
        int[][] dp = new int[word1.length() + 1][word2.length() + 1];

        // base case
        for (int i = 1; i <=word1.length(); i++) {
            dp[i][0] = dp[i-1][0] + 1;
        }
        for (int i = 1; i <=word2.length(); i++) {
            dp[0][i] = dp[0][i-1] + 1;
        }

        // 方向？？？
        for (int i = 1; i <=word1.length(); i++) {
            for (int j = 1; j <= word2.length(); j++) {
                if(word1.charAt(i-1)==word2.charAt(j-1)){
                    dp[i][j] = dp[i-1][j-1];
                }else {
                    dp[i][j] = min(
                            //w1删除变为w2
                            dp[i-1][j] + 1,
                            //w1插入变成w2
                            dp[i][j-1] + 1,
                            // w1替换变成w2
                            dp[i-1][j-1] + 1
                    );
                }
            }
        }
        return dp[word1.length()][word2.length()];

    }

    private int min(int a, int b, int c){
        return Math.min(Math.min(a,b),c);
    }

    @Test
    public void test(){
        System.out.println(minDistance("horse", "ros"));

    }

}
    